//
//  VistorLoginView.swift
//  我的微博
//
//  Created by ZhangChao on 15/9/1.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit

class VistorLoginView: UIView {

    // 设置各个视图的图片和文字
    func setupChildViewInfor(imageName : String?,massage : String){
        
        massageView.text = massage
        
        if let imgName = imageName{
            
            homeIconView.image = UIImage(named: imgName)
            
            // 如果不是首页 必须隐藏转轮
            iconView.hidden = true
            
        }else{
            
            setupAnimation()
        }
    }
    
    // 设置转轮的旋转
    private func setupAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 5
        anim.repeatCount = MAXFLOAT
        
        // 设置动画不会被删除,当iconView被销毁的时候,动画会被自动释放
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // 纯代码开发的时候被调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    // 用sb开发的时候调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    ///  设置界面
    private func setupUI(){
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(massageView)
        addSubview(registerButton)
        addSubview(loginButton)
        // 手动设置布局
        // 一般来说用纯代码开发的时候是不支持自动布局的,所以要把translatesAutoresizingMaskIntoConstraints属性 设置成false
        // 旋转图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal , toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
    
        // 小房子
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -5))
        
        // 消息文字
        massageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: massageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: massageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: massageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 220))
        
        // 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: massageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: massageView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 44))
        
        // 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: massageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: massageView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 44))
        
        // 添加遮罩层(VFL可视化布局)
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-44)-[registerButton]", options: [], metrics: nil, views: ["v" : maskIconView,"registerButton":registerButton]))
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    }
    
    // MARK: - 懒加载控件
    // 图标
    private lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 小屋子
    private lazy var homeIconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 消息文字
    private lazy var massageView :UILabel = {
        let label = UILabel()
        
        label.text = "关注一些人,回头看看有什么惊喜,关注一些人,回头看看有什么惊喜"
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        // 开发中注意不要设置很多黑色,不美观
        label.textColor = UIColor.darkGrayColor()
        
        // 设置字体居中
        label.textAlignment = NSTextAlignment.Center
        
        
        return label
    }()
    // 注册按钮
     lazy var registerButton : UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 添加按钮的点击事件
//        btn.addTarget(self, action: "regestButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    // 登录按钮
    
     lazy var loginButton : UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 添加按钮的点击事件
//        btn.addTarget(self, action: "loginButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // 添加遮罩层(用VFL可视化布局)
    
    private lazy var maskIconView : UIImageView = {
        
        let maskView = UIImageView()
        
        maskView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return maskView
    }()

    // 但是在view里添加点击事件不合适,必须在contronller中才对
//    // 按钮的点击事件
//    func regestButtonClicked (){
//        
//        print("注册")
//    }
//    func loginButtonClicked (){
//        
//        print("登录")
//    }
}